<?php
/*
 * 为了减少不必要的程序消耗，请手动建log目录
 * '/var/log/' . APP_NAME . '/log'
 */
//namespace Peggy;

require_once ( 'Log.php' );


class Logs {

    static $BACKTRACE_LOG_LEVEL = array(
            PEAR_LOG_DEBUG,
            PEAR_LOG_ERR,
            // PEAR_LOG_INFO
    );

    static public function Info( $text, $delReturn = false ) {
        self::WriteLog( PEAR_LOG_INFO, $text, $delReturn );
    }

    static public function Err( $text, $delReturn = false ) {
        self::WriteLog( PEAR_LOG_ERR, $text, $delReturn );
    }

    static public function Warning( $text, $delReturn = false ) {
        self::WriteLog( PEAR_LOG_WARNING, $text, $delReturn );
    }

    static public function Notice( $text, $delReturn = false ) {
        self::WriteLog( PEAR_LOG_NOTICE, $text, $delReturn );
    }

    static public function Debug( $text, $delReturn = false ) {
        self::WriteLog( PEAR_LOG_DEBUG, $text, $delReturn );
    }
    
    static public function Halt( $error ) {
        $e = array();
        if ( !is_array( $error ) ) {
            $trace = debug_backtrace();
            $e['message'] = $error;
            $e['file'] = $trace[0]['file'];
            $e['line'] = $trace[0]['line'];
            ob_start();
            debug_print_backtrace();
            $e['trace'] = ob_get_clean();
        }
        else {
            $e = $error;
        }

        Logs::Debug( 'trace:' . $e['trace'] );
        Logs::Err( 'halt:' . var_export( $e, true ), true );
    }

    /**
     * ログを出力する。
     *
     * @param string $level
     *            PEAR_LOG_EMERG など 0
     *            PEAR_LOG_ALERT
     *            PEAR_LOG_CRIT
     *            PEAR_LOG_ERR
     *            PEAR_LOG_WARNING
     *            PEAR_LOG_NOTICE
     *            PEAR_LOG_INFO
     *            PEAR_LOG_DEBUG 7
     * @param string $category
     *            'Battle' とか
     * @param string $text
     *            出力する文字列
     */
    static public function WriteLog( $level, $text = "", $delReturn = false ) {
        /*
         * $loglevel = CEnv::GetConfig('loglevel');
         * if ( !isset($loglevel) && $loglevel < $level ) {
         * // gv5.confで指定したloglevelより警告度の低いものはログ出力から除外.
         * return;
         * }
         */
        
        if ( $delReturn ) {
            $text = str_replace("\n", '', $text);
        }
        
        // $logdir = CEnv::GetConfig('logdir');
        if ( !isset( $logdir ) ) {
            $app_name = defined( 'APP_NAME' ) or define( 'APP_NAME', 'APP' );
            $logdir = '/var/log/' . APP_NAME . '/log';
        }
        
        if ( isset( $_SERVER["REMOTE_ADDR"] ) ) {
            $category = "{$_SERVER["REMOTE_ADDR"]}:{$_SERVER["REMOTE_PORT"]}";
        }
        
        $date = date( 'Ymd' );
        $logfile_path = $logdir . '/log' . $date . '.log';
        $time_format = '%Y/%m/%d %H:%M:%S';
        $line_format_debug = '';
        
        $backtrace_depth = 2;
        $is_backtrace = in_array( $level, Logs::$BACKTRACE_LOG_LEVEL );
        if ( $is_backtrace ) {
            $line_format_debug = '%1$s %2$s [%3$s] %4$s [%5$s:%6$s]';
        }
        $log = Log::singleton( 'file', $logfile_path, $category, array(
                'timeFormat' => $time_format,
                'lineFormat' => $line_format_debug 
        ) );
        
        if ( $is_backtrace ) {
            $log->setBacktraceDepth( $backtrace_depth );
        }
        
        $log->log( $text, $level );
    }

}
